﻿using System.Collections;
using System.Collections.Generic;
using PEProtocol;
using UnityEngine;
using UnityEngine;
using UnityEngine.UI;

/**
 * 任务窗口
 */
public class TaskWnd : WindowRoot{
    public Transform scrollTrans;
    
    private PlayerData playerData;
    private List<TaskRewardData> trdList = new List<TaskRewardData>();

    protected override void InitWnd(){
        base.InitWnd();
        playerData = GameRoot.Instance.PlayerData;
        RefreshUI();
    }

    public void RefreshUI(){
        trdList.Clear();

        List<TaskRewardData> todoDatas = new List<TaskRewardData>();
        List<TaskRewardData> doneDatas = new List<TaskRewardData>();
        
        //1|0|0
        for (int i = 0; i < playerData.taskArr.Length; i++){
            string[] taskInfo = playerData.taskArr[i].Split('|');
            TaskRewardData trd = new TaskRewardData{
                ID = int.Parse(taskInfo[0]),
                prg = int.Parse(taskInfo[1]),
                taked = taskInfo[2].Equals("1")
            };

            if (trd.taked){
                doneDatas.Add(trd);
            }
            else{
                todoDatas.Add(trd);
            }
        }
        
        trdList.AddRange(todoDatas);
        trdList.AddRange(doneDatas);
        
        //
        for (int i = 0; i < scrollTrans.childCount; i++){
            Destroy(scrollTrans.GetChild(i).gameObject);
        }
        for (int i = 0; i < trdList.Count; i++){
            GameObject go = resSvc.LoadPrefab(PathDefine.TaskItemPrefab);
            go.transform.SetParent(scrollTrans);
            go.transform.localPosition = Vector3.zero;
            go.transform.localScale = Vector3.one;
            go.name = "taskItem_" + i;

            TaskRewardData trd = trdList[i];
            TaskRewardCfg trc = resSvc.GetTaskData(trd.ID);

            SetText(GetTrans(go.transform, "txtName"), trc.taskName);
            SetText(GetTrans(go.transform, "txtPrg"), trd.prg + "/" + trc.count);
            SetText(GetTrans(go.transform, "txtExp"), "奖励：    经验" + trc.exp);
            SetText(GetTrans(go.transform, "txtCoin"), "金币" + trc.coin);
            Image img = GetTrans(go.transform, "prgBar/prgVal").GetComponent<Image>();
            float prgVal = trd.prg * 1.0f / trc.count;
            img.fillAmount = prgVal;
            
            Button btnTake = GetTrans(go.transform, "btnTake").GetComponent<Button>();
            btnTake.onClick.AddListener(() => {
                ClickTakeBtn(go.name);
            });
            
            Transform transComp = GetTrans(go.transform, "imgComp");
            if (trd.taked){
                btnTake.interactable = false;
                SetActive(transComp);
            }
            else{
                SetActive(transComp, false);
                if (trd.prg == trc.count){
                    btnTake.interactable = true;
                }
                else{
                    btnTake.interactable = false;
                }
            }
        }
    }

    private void ClickTakeBtn(string name){
        Debug.Log("click " + name);

        string[] nameArr = name.Split('_');
        int index = int.Parse(nameArr[1]);
        GameMsg msg = new GameMsg{
            cmd = (int) CMD.ReqTakeTaskReward,
            reqTakeTaskReward = new ReqTakeTaskReward{
                rid = trdList[index].ID
            }
        };
        netSvc.SendMsg(msg);

        TaskRewardCfg trc = resSvc.GetTaskData(trdList[index].ID);
        int coin = trc.coin;
        int exp = trc.exp;
        GameRoot.AddTips(Constants.Color("获得奖励: " , TxtColor.Blue) + Constants.Color("金币 +" + coin + " 经验 +" + exp, TxtColor.Green));
    }

    public void ClickCloseBtn(){
        SetWndState(false);
    }
}
